<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { getTicketById, payOrders, cancelOrder } from '@/api/order'
import { showToast, showConfirmDialog } from 'vant'

const route = useRoute()
const router = useRouter()
const order = ref(null)
const loading = ref(false)

const ORDER_STATUS = {
  NOT_PAY: 'TICKET_ORDER_NOT_PAY',
  PAID: 'TICKET_ORDER_PAY',
  CANCELED: 'TICKET_ORDER_CANCEL'
}

const getStatusText = (status) => {
  switch (status) {
    case ORDER_STATUS.NOT_PAY:
      return '待支付'
    case ORDER_STATUS.PAID:
      return '已支付'
    case ORDER_STATUS.CANCELED:
      return '已取消'
    default:
      return '未知状态'
  }
}

const getStatusClass = (status) => {
  switch (status) {
    case ORDER_STATUS.NOT_PAY:
      return 'unpaid'
    case ORDER_STATUS.PAID:
      return 'paid'
    case ORDER_STATUS.CANCELED:
      return 'canceled'
    default:
      return ''
  }
}

const loadOrder = async () => {
  try {
    loading.value = true
    const res = await getTicketById(route.params.id)
    order.value = res.data
  } catch (error) {
    showToast('加载失败')
  } finally {
    loading.value = false
  }
}

const handlePay = async () => {
  try {
    await payOrders(order.value.orderId)
    showToast('支付成功')
    loadOrder()
  } catch (error) {
    showToast('支付失败')
  }
}

const handleCancel = async () => {
  try {
    await showConfirmDialog({
      title: '取消订单',
      message: '确定要取消该订单吗？'
    })
    await cancelOrder(order.value.orderId)
    showToast('取消成功')
    loadOrder()
  } catch (error) {
    if (error !== 'cancel') {
      showToast('取消失败')
    }
  }
}

onMounted(() => {
  loadOrder()
})
</script>

<template>
  <div class="order-detail">
    <van-nav-bar title="订单详情" left-arrow @click-left="$router.back()" />
    
    <van-skeleton title :row="10" :loading="loading" v-if="loading" />
    
    <template v-else-if="order">
      <div class="status-card" :class="getStatusClass(order.status)">
        <div class="status">{{ getStatusText(order.status) }}</div>
      </div>

      <div class="ticket-info">
        <h2>{{ order.ticketInfo?.ticketName }}</h2>
        <div class="price">¥{{ order.price }}</div>
        
        <div class="details">
          <div class="detail-item">
            <span class="label">订单编号</span>
            <span>{{ order.orderId }}</span>
          </div>
          <div class="detail-item">
            <span class="label">下单时间</span>
            <span>{{ order.createTime }}</span>
          </div>
        </div>
      </div>

      <div class="action-bar">
        <template v-if="order.status === ORDER_STATUS.NOT_PAY">
          <van-button plain type="danger" @click="handleCancel">取消订单</van-button>
          <van-button type="primary" @click="handlePay">立即支付</van-button>
        </template>
      </div>
    </template>
  </div>
</template>

<style scoped lang="scss">
.order-detail {
  min-height: 100vh;
  background: #f8f8f8;

  .status-card {
    padding: 24px;
    color: #fff;
    position: relative;
    overflow: hidden;
    
    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background: linear-gradient(45deg, rgba(255,255,255,0.1), rgba(255,255,255,0));
    }

    .status {
      font-size: 24px;
      font-weight: bold;
      margin-bottom: 8px;
      position: relative;
    }

    &.unpaid {
      background: linear-gradient(135deg, #ff976a, #ff6b6b);
    }
    
    &.paid {
      background: linear-gradient(135deg, #07c160, #1989fa);
    }
    
    &.canceled {
      background: linear-gradient(135deg, #969799, #646566);
    }
  }

  .ticket-info {
    background: #fff;
    margin: 12px;
    padding: 16px;
    border-radius: 8px;

    h2 {
      margin: 0 0 16px;
      font-size: 18px;
    }

    .price {
      color: #ff6b6b;
      font-size: 20px;
      font-weight: bold;
      margin-bottom: 16px;
    }

    .details {
      .detail-item {
        display: flex;
        justify-content: space-between;
        margin-bottom: 12px;
        font-size: 14px;

        .label {
          color: #666;
        }
      }
    }
  }

  .action-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: flex-end;
    gap: 12px;
    padding: 8px 16px;
    background: #fff;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  }
}
</style> 